package com.ruoyi.system.task;

import com.ruoyi.system.domain.PlcItem;
import com.ruoyi.system.domain.WcsPower;
import com.ruoyi.system.mapper.PlcItemMapper;
import com.ruoyi.system.mapper.WcsPowerMapper;
import com.ruoyi.system.service.IPlcItemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class WcsPowerTask {
    private Logger logger = LoggerFactory.getLogger(WcsPowerTask.class);

    @Autowired
    private PlcItemMapper plcItemMapper;

    @Autowired
    private IPlcItemService plcItemService;

    @Autowired
    private WcsPowerMapper wcsPowerMapper;

//    @Scheduled(cron="0/5 * * * * ?")//从第0秒没5秒
//    @Scheduled(cron="0 0/2 * * * ? ")//没2分钟执行一次
    @Scheduled(cron="0 50 23 * * ?")//每天23点50分保存数据
    public void saveCurrerPower() {
        logger.info("电能统计开始！！");
//        System.out.println(new Date());
//        List<WcsPower> list = wcsPowerMapper.selectWcsPowerListForExport(new Date());
//        System.out.println(list);
        saveData("DN");//电能信息
        saveData("DY");//电压信息
        saveData("DL");//电流信息
    }

    /**
     * 根据参数保存不同的电信息
     * @param dataType
     */
    public void saveData(String dataType){
        //电能数据保存
        PlcItem plcItem = new PlcItem();
        plcItem.setItemNo(dataType);
        List<PlcItem> plcItemList = plcItemMapper.selectPlcItemList(plcItem);

        for(PlcItem p : plcItemList){
            WcsPower wcsPower = new WcsPower();
            wcsPower.setDeviceNo(p.getItemNo());
            wcsPower.setDataType(dataType);
            wcsPower.setValue(Double.parseDouble(p.getItemValue()));
//            System.out.println(wcsPower);
            wcsPowerMapper.insertWcsPower(wcsPower);
        }
    }
}
